package com.andy.qhb.action;

import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Component;

import com.andy.qhb.dao.DaoUtils;
import com.andy.qhb.entity.UsrGroups;
import com.andy.qhb.entity.UsrUser;
import com.andy.qhb.game.GameUser;
import com.andy.qhb.game.Global;
import com.andy.qhb.utils.GmAction;
import com.andy.qhb.utils.SendUtil;
import com.andy.qhb.vo.HFriend;
import com.andy.qhb.websocket.MySocketSession;

/**
 * 邀请入群
 * @author cuiwm
 *
 */
@Component(GmAction.ACTION_PREFIX+GmAction.ACTION_114)
public class Action114 implements IWebAction{

	@Override
	public void doAction(MySocketSession session, Map<String, Object> data) {
		GameUser user = session.getUser();
		Long groupId = MapUtils.getLong(data,"groupId");
		Long userId = MapUtils.getLong(data,"userId");
		
		UsrGroups usrGroup = Global.getUsrGroup(groupId);
		if(usrGroup == null || !usrGroup.getCid().equals(user.getId())) return;
		
		UsrUser usrUser = DaoUtils.getOneSql(UsrUser.class, "select id,nick_name,icon from usr_user where id=?", userId);
		if(usrUser == null) {
			SendUtil.sendError(session, GmAction.ACTION_114, "玩家不存在");
			return;
		}
		usrGroup.addMember(userId, usrUser.getNickName(),usrUser.getIcon());
		DaoUtils.update(usrGroup);
		SendUtil.send114(session,groupId,usrUser);
		
		MySocketSession tarSession = Global.getSession(userId);
		if(tarSession != null) {
			GameUser tarUser = tarSession.getUser();
			if(tarUser != null) {
				tarUser.getGroups().add(new HFriend(groupId, usrGroup.getName(),null));
				SendUtil.send113(tarSession, usrGroup);
			}
		}
		
	}

	
	
	
}
